package com.zdkj.admin.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zdkj.admin.api.dto.RoleMenuDTO;
import com.zdkj.admin.api.entity.SysRole;
import com.zdkj.admin.service.SysRoleMenuService;
import com.zdkj.admin.service.SysRoleService;
import com.zdkj.common.core.util.R;
import com.zdkj.common.log.annotation.SysLog;
import io.swagger.annotations.Api;
import lombok.AllArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * @author cui_com
 * @date 2017/11/5
 */
@RestController
@AllArgsConstructor
@RequestMapping("/role")
@Api(value = "role", tags = "角色管理模块")
public class SysRoleController {
    private final SysRoleService sysRoleService;
    private final SysRoleMenuService sysRoleMenuService;

    /**
     * 通过ID查询角色信息
     *
     * @param id ID
     * @return 角色信息
     */
    @GetMapping("/{id}")
    public R getById(@PathVariable Integer id) {
        return R.ok(sysRoleService.getById(id));
    }

    /**
     * 添加角色
     *
     * @param sysRole 角色信息
     * @return success、false
     */
    @SysLog("添加角色")
    @PostMapping
    @PreAuthorize("@pms.hasPermission('sys_role_add')")
    public R save(@Valid @RequestBody SysRole sysRole) {
        return R.ok(sysRoleService.save(sysRole));
    }

    /**
     * 修改角色
     *
     * @param sysRole 角色信息
     * @return success/false
     */
    @SysLog("修改角色")
    @PutMapping
    @PreAuthorize("@pms.hasPermission('sys_role_edit')")
    public R update(@Valid @RequestBody SysRole sysRole) {
        return R.ok(sysRoleService.updateById(sysRole));
    }

    /**
     * 删除角色
     *
     * @param id
     * @return
     */
    @SysLog("删除角色")
    @DeleteMapping("/{id}")
    @PreAuthorize("@pms.hasPermission('sys_role_del')")
    public R removeById(@PathVariable Integer id) {
        return R.ok(sysRoleService.removeRoleById(id));
    }

    /**
     * 获取角色列表
     *
     * @return 角色列表
     */
    @GetMapping("/list")
    public R listRoles() {
        return R.ok(sysRoleService.list(Wrappers.emptyWrapper()));
    }

    /**
     * 分页查询角色信息
     *
     * @param page 分页对象
     * @return 分页对象
     */
    @GetMapping("/page")
    public R getRolePage(Page page, SysRole sysRole) {		
		String roleName = sysRole.getRoleName();
		if(StringUtils.isNotEmpty(roleName)){
			sysRole.setRoleName(null);
			//增加角色编号模糊搜索功能
		    QueryWrapper<SysRole> queryWrapper = Wrappers.query(sysRole);
            queryWrapper.lambda().like(SysRole::getRoleName,roleName);//角色名称模糊匹配
			return R.ok(sysRoleService.page(page, queryWrapper));
        }
		
        return R.ok(sysRoleService.page(page, Wrappers.query(sysRole)));
    }

    /**
     * 更新角色菜单
     *
     * @return success、false
     */
    @SysLog("更新角色菜单")
    @PutMapping("/menu")
    @PreAuthorize("@pms.hasPermission('sys_role_perm')")
    public R saveRoleMenus(@RequestBody RoleMenuDTO roleMenuDTO) {
        SysRole sysRole = sysRoleService.getById(roleMenuDTO.getRoleId());
        return R.ok(sysRoleMenuService.saveRoleMenus(sysRole.getRoleCode(), roleMenuDTO.getRoleId(), roleMenuDTO.getMenuIds()));
    }

    /**
     * 通过角色ID 查询角色列表
     *
     * @param roleIdList 角色ID
     * @return
     */
    @PostMapping("/getRoleList")
    public R getRoleList(@RequestBody List<String> roleIdList) {
        return R.ok(sysRoleService.listByIds(roleIdList));
    }

    /**
     * 通过角色ID 查询用户列表  关联用户信息
     *
     * @param roleIdList 角色IDs
     * @return
     */
    @PostMapping("/getRoleSUserList")
    public R getRoleUsers(Page page, @RequestBody List<String> roleIdList) {
        return R.ok(sysRoleService.listByIdsLinkUser(page, roleIdList));
    }


}
